home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / ym_utils / pc_copy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-08  |  7.2 KB  |  337 lines

  1. /* UnIX to MS-DOS file converter  by
  2.         Matthew B. Hornbeck, Director of Technical Services
  3.         Copyright 1989 by Young Minds, Incorporated
  4.         August 8, 1989.
  5. File : PC_COPY.C
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <time.h>
  11.  
  12. #define FALSE    0
  13. #define TRUE    !FALSE
  14.  
  15. #ifdef __MSDOS__
  16.  
  17. #include <dir.h>
  18. #include <dos.h>
  19. #include <alloc.h>
  20. #include <stdlib.h>
  21. #include <sys\stat.h>
  22.  
  23. #define malloc    farmalloc
  24. #define free    farfree
  25.  
  26. #define SLASH        '\\'
  27.  
  28. #else
  29.  
  30. #include <malloc.h>
  31. #include <sys/types.h>
  32. #include <dirent.h>
  33. #include <sys/stat.h>
  34.  
  35. #define mystat        lstat
  36.  
  37. #define SLASH        '/'
  38.  
  39. #endif
  40.  
  41. #ifdef __MSDOS__
  42. char *prog_name (char *program_name)
  43. #else
  44. char *prog_name (program_name)
  45. char *program_name;
  46. #endif
  47.  
  48. {    unsigned int len;
  49.  
  50.     len = strlen (program_name) - 1;
  51.     program_name = program_name + len;
  52.     while ((len != 0) && (*program_name != SLASH))
  53.       {
  54. #ifdef __MSDOS__
  55.         if (*program_name == '.')
  56.             *program_name = '\0';
  57. #endif
  58.         program_name --;
  59.         len --;
  60.       }
  61.     if (*program_name == SLASH)
  62.         program_name ++;
  63.     return program_name;
  64. }
  65.  
  66. #ifdef __MSDOS__
  67. int copy_file (char *in_name, char *out_name)
  68. #else
  69. int copy_file (in_name, out_name)
  70. char *in_name;
  71. char *out_name;
  72. #endif
  73.  
  74. {    FILE *in, *out;
  75.     int chr;
  76.  
  77. #ifdef __MSDOS__
  78.     if ((in = fopen (in_name, "rb")) == NULL)
  79. #else
  80.     if ((in = fopen (in_name, "r")) == NULL)
  81. #endif
  82.       {    fprintf (stderr, "Unable to open input file %s!\n", in_name);
  83.         return FALSE;
  84.       }
  85. #ifdef __MSDOS__
  86.     if ((out = fopen (out_name, "wb")) == NULL)
  87. #else
  88.     if ((out = fopen (out_name, "w")) == NULL)
  89. #endif
  90.       {    fprintf (stderr, "Unable to open output file %s!\n", out_name);
  91.         fclose (in);
  92.         return FALSE;
  93.       }
  94.     while ((chr = getc (in)) != EOF)
  95.       {    if ((chr == '\n') && (putc ('\r', out) == EOF))
  96.           {    fprintf (stderr, "Error writing to output file %s!\n", out_name);
  97.             perror ("Problem");
  98.             fclose (out);
  99.             fclose (in);
  100.             return FALSE;
  101.           }
  102.         if (putc (chr, out) == EOF)
  103.           {    fprintf (stderr, "Error writing to output file %s!\n", out_name);
  104.             perror ("Problem");
  105.             fclose (out);
  106.             fclose (in);
  107.             return FALSE;
  108.           }
  109.       }
  110.     fclose (out);
  111.     fclose (in);
  112.     return TRUE;
  113. }
  114.  
  115. #ifdef __MSDOS__
  116.  
  117. char *get_name (char *dir_name, int has_wild)
  118.  
  119. {    static struct ffblk *fptr = NULL;
  120.     static struct ffblk fblk;
  121.     static char tmpstr [2048];
  122.  
  123.     if (dir_name != NULL)
  124.       {    if (has_wild)
  125.             strcpy (tmpstr, dir_name);
  126.         else
  127.             sprintf (tmpstr, "%s%c*.*", dir_name, SLASH);
  128.         if (findfirst (tmpstr, &fblk, -1 ^ FA_LABEL) == 0)
  129.           {    fptr = &fblk;
  130.             return fblk.ff_name;
  131.           }
  132.         fptr = NULL;
  133.         return NULL;
  134.       }
  135.     if (fptr == NULL)
  136.         return NULL;
  137.     if (findnext (&fblk) == 0)
  138.         return fblk.ff_name;
  139.     fptr = NULL;
  140.     return NULL;
  141. }
  142.  
  143. #else
  144.  
  145. char *get_name (dir_name, has_wild)
  146. char *dir_name;
  147. int has_wild;
  148.  
  149. {    static DIR *dirp = NULL;
  150.     struct dirent *dir;
  151.  
  152.     if (dir_name != NULL)
  153.       {    if ((dirp = opendir (dir_name)) == NULL)
  154.             return NULL;
  155.       }
  156.     if ((dir = readdir (dirp)) == NULL)
  157.       {    closedir (dirp);
  158.         dirp = NULL;
  159.         return NULL;
  160.       }
  161.     return dir->d_name;
  162. }
  163.  
  164. #endif
  165.  
  166. typedef struct dir_element {
  167.     struct dir_element *next;
  168.     char *cd_path;
  169.     char *new_path;
  170. } dir_elem;
  171.  
  172. static dir_elem *head = NULL, *tail = NULL;
  173.  
  174. #ifdef __MSDOS__
  175. int push_dir (char *cd_path, char *new_path)
  176. #else
  177. int push_dir (cd_path, new_path)
  178. char *cd_path, *new_path;
  179. #endif
  180.  
  181. {    dir_elem *tmp;
  182.  
  183.     if (head == NULL)
  184.       {    if ((head = tmp = (dir_elem *) malloc (sizeof (dir_elem))) == NULL)
  185.           {    fprintf (stderr, "Unable to allocate dir_element buffer!\n");
  186.             return FALSE;
  187.           }
  188.       }
  189.     else
  190.       {    if ((tail->next = tmp = (dir_elem *) malloc (sizeof (dir_elem))) == NULL)
  191.           {    fprintf (stderr, "Unable to allocate dir_element buffer!\n");
  192.             return FALSE;
  193.           }
  194.       }
  195.     tmp->next = NULL;
  196.     if ((tmp->cd_path = (char *) malloc (strlen (cd_path) + 1)) == NULL)
  197.       {    fprintf (stderr, "Unable to allocate cd_path of dir_element!\n");
  198.         return FALSE;
  199.       }
  200.     strcpy (tmp->cd_path, cd_path);
  201.     if ((tmp->new_path = (char *) malloc (strlen (new_path) + 1)) == NULL)
  202.       {    fprintf (stderr, "Unable to allocate new_path of dir_element!\n");
  203.         return FALSE;
  204.       }
  205.     strcpy (tmp->new_path, new_path);
  206.     tail = tmp;
  207.     return TRUE;
  208. }
  209.  
  210. #ifdef __MSDOS__
  211. dir_elem *dequeue_dir (void)
  212. #else
  213. dir_elem *dequeue_dir ()
  214. #endif
  215.  
  216. {    dir_elem *tmp;
  217.  
  218.     if (head == NULL)
  219.       {    tail = NULL;
  220.         return NULL;
  221.       }
  222.     tmp = head;
  223.     head = tmp->next;
  224.     tmp->next = NULL;
  225.     return tmp;
  226. }
  227.  
  228. #ifdef __MSDOS__
  229. void free_dir (dir_elem *dir)
  230. #else
  231. void free_dir (dir)
  232. dir_elem *dir;
  233. #endif
  234.  
  235. {
  236.     free (dir->cd_path);
  237.     free (dir->new_path);
  238.     free (dir);
  239. }
  240.  
  241. #ifdef __MSDOS__
  242. int proc_dir (char *cd_path, char *new_path, int recurse)
  243. #else
  244. int proc_dir (cd_path, new_path, recurse)
  245. char *cd_path;
  246. char *new_path;
  247. int recurse;
  248. #endif
  249.  
  250. {    struct stat statbuf;
  251.     char *fname, *ptr, file_name [1024], new_name [1024];
  252.     int i, has_wild;
  253.  
  254.     has_wild = FALSE;
  255. #ifdef __MSDOS__
  256.     if (fnsplit (cd_path, new_name, new_name, new_name, new_name) & WILDCARDS)
  257.         has_wild = TRUE;
  258. #endif
  259.     if (!has_wild && (stat (cd_path, &statbuf) == -1))
  260.       {    fprintf (stderr, "Unable to get status of %s!\n", cd_path);
  261.         return FALSE;
  262.       }
  263.     if (!has_wild && ((statbuf.st_mode & S_IFREG) != 0))
  264.       {    i = strlen (cd_path) - 1;
  265.         for (fname = cd_path + i; (i != 0) && (*fname != SLASH); i --)
  266.             fname --;
  267.         sprintf (file_name, "%s%c%s", new_path, SLASH, fname);
  268.         return copy_file (cd_path, file_name);
  269.       }
  270.     else if (!has_wild && ((statbuf.st_mode & S_IFDIR) == 0))
  271.       {    fprintf (stderr, "Don't understand file type of %s!\n", cd_path);
  272.         return FALSE;
  273.       }
  274.     if ((fname = get_name (cd_path, has_wild)) == NULL)
  275.       {    fprintf (stderr, "No such file %s!\n", cd_path);
  276.         return FALSE;
  277.       }
  278.     if (has_wild)
  279.       {    i = strlen (cd_path) - 1;
  280.         for (ptr = cd_path + i; (i != 0) && (*ptr != SLASH); i --)
  281.             ptr --;
  282.         *ptr = '\0';
  283.       }
  284.     do
  285.       {    if ((strcmp (fname, ".") == 0) || (strcmp (fname, "..") == 0))
  286.             continue;
  287.         sprintf (new_name, "%s%c%s", cd_path, SLASH, fname);
  288.         if (stat (new_name, &statbuf) == -1)
  289.           {    fprintf (stderr, "Couldn't get status of %s!\n", new_name);
  290.             continue;
  291.           }
  292.         sprintf (file_name, "%s%c%s", new_path, SLASH, fname);
  293.         if ((statbuf.st_mode & S_IFREG) != 0)
  294.             copy_file (new_name, file_name);
  295.         else if ((statbuf.st_mode & S_IFDIR) != 0)
  296.           {    if (!recurse)
  297.                 continue;
  298. #ifdef __MSDOS__
  299.             mkdir (file_name);
  300. #else
  301.             mkdir (file_name, 0777);
  302. #endif
  303.             push_dir (new_name, file_name);
  304.           }
  305.         else
  306.             fprintf (stderr, "Unknown file type for %s!\n", new_name);
  307.       } while ((fname = get_name (NULL, FALSE)) != NULL);
  308.     return TRUE;
  309. }
  310.  
  311. #ifdef __MSDOS__
  312. int main (int argc, char *argv [])
  313. #else
  314. int main (argc, argv)
  315. int argc;
  316. char *argv [];
  317. #endif
  318.  
  319. {    dir_elem *cur_dir;
  320.     int recurse;
  321.  
  322.     recurse = FALSE;
  323.     if ((argc == 3) && ((strcmp (argv [1], "-r") == 0) || (strcmp (argv [1], "-R") == 0)))
  324.         recurse = TRUE;
  325.     else if (argc != 2)
  326.       {    fprintf (stderr, "Usage : %s [-r] cd_pathname\n", prog_name (argv [0]));
  327.         exit (1);
  328.       }
  329.     push_dir (argv [argc - 1], ".");
  330.     while ((cur_dir = dequeue_dir ()) != NULL)
  331.       {    proc_dir (cur_dir->cd_path, cur_dir->new_path, recurse);
  332.         free_dir (cur_dir);
  333.       }
  334.     return 0;
  335. }
  336.  
  337.